//
//  SpeechSynthesizerViewController.m
//  AVFoundation
//
//  Created by bao on 2018/6/21.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "SpeechSynthesizerViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface SpeechSynthesizerViewController () <AVSpeechSynthesizerDelegate>

@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation SpeechSynthesizerViewController

- (void)dealloc
{
    DCLog(@"%s", __func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.synthesizer = [[AVSpeechSynthesizer alloc] init];
    self.synthesizer.delegate = self;
//    NSLog(@"%@", [AVSpeechSynthesisVoice speechVoices]);
}

- (IBAction)play
{
    NSString *words = self.textLabel.text;
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:words];
    utterance.pitchMultiplier = 1.0;
    utterance.volume = 1.0;
//    utterance.preUtteranceDelay = 5.0;
//    utterance.postUtteranceDelay = 5.0;
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    [self.synthesizer speakUtterance:utterance];
}

#pragma mark - AVSpeechSynthesizerDelegate

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
{
    DCLog(@"didStartSpeechUtterance");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
    DCLog(@"didFinishSpeechUtterance");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
{
    DCLog(@"didPauseSpeechUtterance");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance
{
    DCLog(@"didContinueSpeechUtterance");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
    DCLog(@"didCancelSpeechUtterance");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
    DCLog(@"willSpeakRangeOfSpeechString---%@", NSStringFromRange(characterRange));
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
